$(function() {
	let relearr = [];
	let removerelearr = [];
	let tishi;
	let fi=1;
	let page=1;
	let main1=[[ //表头
		{type:'checkbox', fixed: 'left'}
        ,{field: 'number', title: '车牌号',  sort: true, fixed: 'left', templet: '#driverId'}
        ,{field: 'car_type_name', title: '车型',  sort: true}
       ,{ title: '操作', toolbar:'#operation',fixed: 'right'}
        
    ]];
    let main2=[[ //表头
		{type:'checkbox', fixed: 'left'}
        ,{field: 'number', title: '车牌号',  sort: true, fixed: 'left', templet: '#driverId'}
        ,{field: 'car_type_name', title: '车型',  sort: true}
        
        ,{ title: '操作',minWidth:250, toolbar:'#operation1',fixed: 'right'}
    ]];
    let data={};
	data.page=page;
	data.pagenum=10;
	{if $Think.get.number}
	data.number='{$Think.get.number}'
	{/if}
	{if $Think.get.type}
	data.type='{$Think.get.type}'
	{/if}
	console.log(data)
	requestnorelevance(data);
	requestrelevance();
	
	$('.morerelevance').on('click',function(){
		var checkStatus = table.checkStatus('no-relevance')
	      ,data = checkStatus.data;
	    let arr=[];
	    for(var i=0;i<data.length;i++){
	    	arr.push(data[i].car_id)
	    }
	    proceedrelevance('{$Think.get.driver_id}', arr)
	})
	
	//请求所有车辆
function requestnorelevance(data) {
		let norelevanceload = layer.load(0, {
			shade:  [0.3,'#fff']
		});
		ajax(api.driver.norelevance,data,'post').done(function(res){
			layer.close(norelevanceload);
			table.render({
				elem: '#no-relevance',
				cellMinWidth: 80,
				cols: main1,
				data: res.data.list,
				limit: data.pagenum,
				even: true
			});
			if (fi == 1) {
				laypage.render({
					elem: 'page',
					count: res.data.count,
					layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
					theme: '#1E9FFF',
					jump: function (obj, first) {
						if (!first) {
							page = obj.curr
							data.page = page
							pagenum = obj.limit
							data.pagenum = pagenum
							requestnorelevance(data)
						} else {

						}
					}
				});
				fi = 2
			};
			table.on('tool(no-relevance)', function (obj) {
    				var data1 = obj.data; //获得当前行数据
    				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
    				var tr = obj.tr; //获得当前行 tr 的DOM对象
    				
    				if (layEvent === 'relevance') { //查看
						console.log(1)
							let that=this
							tishi=layer.open({
								type: 1,
								title: false,
								
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否关联</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									var arr=[];
									arr.push(data1.car_id)
									proceedrelevance("{$Think.get.driver_id}",arr)
									layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
    				}
    			});
		}).fail(function(){
			layer.close(norelevanceload)
		})
//			
		
	}
	//请求关联车辆
	function requestrelevance(res) {
		let relevanceload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		ajax(api.driver.relevance,{
			driver_id: "{$Think.get.driver_id}",
		},'post').done(function(res){
			console.log(res)
			layer.close(relevanceload);
				console.log(res);
				let relevance = '';
				
				if(res.code == 1) {
					table.render({
						elem: '#relevance',
						cellMinWidth: 80,
						cols: main2,
						data: res.data,
						limit: data.pagenum,
						even: true
					});
					table.on('tool(relevance)', function (obj) {
	    				var data1 = obj.data; //获得当前行数据
	    				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	    				var tr = obj.tr; //获得当前行 tr 的DOM对象
	    				
	    				if (layEvent === 'norelevance') { //查看
							let that=this
						tishi=layer.open({
								type: 1,
								title: false,
								
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否解除关联</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									removerelearr = []
									removerelearr.push(data1.car_id)
									removerelevance("{$Think.get.driver_id}", removerelearr)
									layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
	    				}else if (layEvent === 'default') { 
	    					tishi=layer.open({
								type: 1,
								title: false,
								//offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
									//,
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否设为默认车辆</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									 isdefault("{$Think.get.driver_id}", data1.car_id);
									 layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
	    				}
	    			});
				}else{
					Err(res.msg)
				}
		}).fail(function(){
			
		})
			

	}

	//绑定车辆
	function proceedrelevance(driverid, main) {
		let proceedload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		var car_id_arr=JSON.stringify(main)
		ajax(api.driver.bindCar,{
			driver_id: driverid,
			car_id: car_id_arr
		},'post').done((res)=>{
			layer.close(proceedload);
					console.log(res)
					if(res.code==1){
					Msg('绑定成功')
					requestnorelevance(1)
					requestrelevance()
					requestnorelevance(page)
					}else{
						Err(res.msg)
					}
		}).fail(function(){
			Err('请求失败');
			layer.close(proceedload);
		})
			
	}
	//解除绑定车辆
	function removerelevance(driverid, caridarr) {
		let proceedload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		caridarr1 =JSON.stringify(caridarr)
		ajax(api.driver.delBindCar,{
			driver_id: driverid,
			car_id: caridarr1,
		},'post').done((res)=>{
			layer.close(proceedload);
				console.log(res)
				if(res.code==1){
					Msg('解除成功')
					requestnorelevance(1)
					requestrelevance()
				}else{
					Err(res.msg)
				}
		}).fail(()=>{
			layer.close(proceedload);
			Err('请求失败')
		})
		
	}
	//设置默认车
	function isdefault(driverid, carid){
		let isdefaultload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		ajax(api.driver.setDefaultCarP,{
			driver_id: driverid,
			car_id: carid,
		},'post').done((res)=>{
			layer.close(isdefaultload);
			if(res.code==1){
				Msg('设置完成')
				requestrelevance();
				requestnorelevance(page)
			}else{
				Err(res.msg)
			}
		}).fail(()=>{
			layer.close(isdefaultload);
			Err('请求失败')
		})
		
	}
})